﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager
{
    /// <summary>
    /// Interaction logic for ucAirConditionWoodDoor.xaml
    /// </summary>
    public partial class ucAirConditionWoodDoor : UserControl
    {
        public ucAirConditionWoodDoor()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            reload();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            tb_equipment ep = new tb_equipment();
            ep.equipment_name = cbxEquipment.Text;
            ep.energy = Convert.ToInt16(txtEnergy.Text);
            LibraryManagerInCom.addEquipment(ep);
            enableButtons();
            reload();

        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxEquipment.Text = "";
            txtEnergy.Text = "";
            disableButtons();
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_equipment eq2 = LibraryManagerInCom.copyEquipment((tb_equipment)cbxEquipment.SelectedItem);
            cbxEquipment.Text = eq2.equipment_name;
            txtEnergy.Text = eq2.energy.ToString();
            disableButtons();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            tb_equipment eq = (tb_equipment)cbxEquipment.SelectedItem;
            LibraryManagerInCom.delEquipment(eq);
            reload();
        }

        public void loadData()
        {
            cbxEquipment.ItemsSource = LibraryManagerInCom.EquipmentList;
            cbxEquipment.DisplayMemberPath = "equipment_name";
            cbxEquipment.SelectedIndex = 0;
        }
        public void reload()
        {
            cbxEquipment.ItemsSource = null;
            cbxEquipment.Items.Clear();
            foreach (var item in LibraryManagerInCom.EquipmentList)
            {
                cbxEquipment.Items.Add(item);
            }
            cbxEquipment.DisplayMemberPath = "equipment_name";
            cbxEquipment.SelectedIndex = 0;
        }

        private void enableButtons()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;
        }
        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
            enableButtons();
        }

        private void cbxEquipment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            tb_equipment eq = (tb_equipment)cbxEquipment.SelectedItem;
            if (eq != null)
            {
                txtEnergy.Text = eq.energy.ToString();
            }
            
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}
